<template>
  <div class="mHeader">
    <div class="m-title">
      <client-only>
        <el-row>
          <!-- xs <768  sm 768-992 md > 992 -->
          <el-col :xs="12" :sm="5" :md="4">
            <div class="logo-wrap" @click="goHome">
   
              <span class="logo-txt">常用工具</span>
            </div>
          </el-col>
          <el-col :xs="0" :sm="16" :md="16">
            <el-menu
              :default-active="defaultMenuActiveIndex"
              class="el-menu hidden-xs-only"
              mode="horizontal"
              background-color="white"
              text-color="#999"
              active-text-color="#333"
              :router="true"
            >
              <template v-for="(item, mIndex) in menuOptions2">
                <el-sub-menu
                  :index="item.key"
                  v-if="item.children?.length > 0"
                  :key="item.key"
                >
                  <template #title>{{ item.label }}</template>
                  <el-menu-item
                    :index="chindItem.key"
                    v-for="chindItem in item.children"
                  >
                    <template #title>{{ chindItem.label }}</template>
                  </el-menu-item>
                </el-sub-menu>
                <el-menu-item v-else :index="item.key" :key="mIndex">
                  <template #title>{{ item.label }}</template>
                </el-menu-item>
              </template>
            </el-menu>
          </el-col>
          <el-col :xs="12" :sm="3" :md="4">
            <div class="user">
              
              <!-- <div class="hidden-sm-and-up menu-icon u-item">
                <div @click="toggleShowMenu">更多</div>
                <el-icon size="28" ><Menu /></el-icon>
              </div> -->
              <!-- <div class="u-item u-login" v-if="userInfo.phone && token">{{ userInfo.phone }}</div> -->
              <div v-if="token" class="u-item">
                <el-dropdown @command="clickDropDown">
                  <div class="drop-avatar">
                    <!-- <el-avatar :size="36" :src="avatarImg" /> -->
                    <img class="avatar" :src="avatarImg"/>
                  </div>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item command="changePassword"
                        >修改密码</el-dropdown-item
                      >

                      <el-dropdown-item command="contractAdmin"
                        >管理员</el-dropdown-item
                      >
                      <el-dropdown-item command="loginOut"
                        >退出登录</el-dropdown-item
                      >
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div>
              <template v-else>
                <!-- <div class="u-item u-register" @click="becomeVip">注册</div> -->
                <div class="u-item u-login" @click="goLogin">登录</div>
                <div class="u-item u-login u-vip hidden-sm-only" @click="becomeVip">
                  <div>注册</div>
                </div>
                <!-- <div class="u-item u-register" @click="goRegister">注册</div>   -->
              </template>
            </div>
          </el-col>
        </el-row>
        <el-drawer
          v-model="drawerVisible"
          direction="ltr"
          title="菜单"
          size="50%"
        >
          <p
            class="link-item"
            v-for="(item, index) in plainMenuOptions"
            :key="index"
          >
            <el-link type="primary" :href="item.key">{{
              item.label
            }}</el-link>
          </p>
        </el-drawer>
      </client-only>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted,onUpdated,watch } from 'vue'
import { Menu } from '@element-plus/icons-vue'
import { useRouter, useRoute } from 'vue-router'
import { loadStore } from '@/store'
import { storeToRefs } from 'pinia'
import { ElMessage, ElMessageBox } from 'element-plus'
import { adminDialog } from '@/components/adminDialog'
let dogPng = 'https://gg-common.oss-cn-beijing.aliyuncs.com/images/poi/dog.png'
let tzPng = 'https://gg-common.oss-cn-beijing.aliyuncs.com/images/poi/tuzi.jpg'
import { menuOptions2 } from '@/utils/menu.js'
const userStore = loadStore()
const { token, userInfo } = storeToRefs(userStore)
// console.log('userInfo:',userInfo.value)

const avatarImg = ref(tzPng)
if(userInfo && userInfo.value){
  avatarImg.value = userInfo.value.role ==='admin' ?  dogPng : tzPng
}



const router = useRouter()
const route = useRoute()
const headerMenuOptions2 = ref(menuOptions2)

watch(route,()=>{
  const userStore = loadStore()
  const { userInfo } = storeToRefs(userStore)
  // console.log('route:',route)
  if(userInfo && userInfo.value){
    avatarImg.value = userInfo.value.role ==='admin' ?  dogPng : tzPng
  }
  // console.log('avatarImg.value:',avatarImg.value)
})

let plainMenuOptions = []

menuOptions2.forEach((item) => {
  if (item.children) {
    plainMenuOptions = plainMenuOptions.concat(item.children)
  } else {
    plainMenuOptions.push(item)
  }
})
const drawerVisible = ref(false)

// 高亮标签页

const matchedRoute = route.matched
// console.log('matchedRoute:',matchedRoute)
let matchMenu = headerMenuOptions2.value.find((menu) => {
  return matchedRoute.some((matchRItem) => {
    return matchRItem.path === menu.key
  })
})


let defaultMenuActiveIndex = ref(matchMenu && matchMenu.key || '/')

function toggleShowMenu() {
  drawerVisible.value = !drawerVisible.value
}

function goRegister(){
  router.push({
    path: '/login?actionType=register'
  })
}
function goLogin() {
  router.push({
    path: '/login'
  })
}

function clickDropDown(command) {
  if (command === 'changePassword') {
    router.push({ path: '/login', query: { actionType: 'update' } })
  } else if (command === 'loginOut') {
    userStore.loginOut()
    ElMessage({
      type: 'success',
      message: '退出成功'
    })
    router.push({
      path: '/'
    })
  } else if (command === 'contractAdmin') {
    ElMessageBox.alert('管理员微信：Excel656', '提示').catch(() => {})
  }
}



function goHome() {
  router.push({ path: '/' })
}



function becomeVip() {
  adminDialog.show()
}


</script>
<style lang="scss" scoped>
.mHeader {
  width: 100%;
  height: 60px;
  box-sizing: border-box;
  background-color: white;
  font-weight: bold;
  
  
  .m-title {
    width: 100%;
    height: 100%;
    padding: 0px 20px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .logo-wrap {
      display: flex;
      align-items: center;
      height: 100%;
      text-decoration: none;
      cursor: pointer;
      .logo {
        width: 40px;
        height: 40px;
      }
      .logo-txt {
        font-size: 16px;
        font-weight: bold;
        margin-left: 10px;
        text-decoration: none;
        color: #333;
      }
    }
    .el-menu {
      height: 60px;
      .el-menu-item {
        font-size: 16px;
        border: none !important;
      }
      :deep(.el-sub-menu) {
        .el-sub-menu__title {
          font-size: 16px;
          border: none !important;
        }
      }
    }

    .user {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      box-sizing: border-box;
      color: #999;
      text-align: center;
      text-decoration: none;
      cursor: pointer;
      font-size: 16px;
      -webkit-tap-highlight-color: transparent;
      .menu-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        color: #999;
        &:hover {
          color: #333;
        }
      }
      .u-item {
        padding: 0px 10px;
        height: 60px;
        line-height: 60px;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        &:hover {
          background: white;
          color: black;
        }
        .drop-avatar{
          .avatar{
            width: 36px;
            height: 36px;
            border-radius: 18px;
          }
        }
      }
    }
  }
  .m-subTitle{
    padding:10px;
  }
}
.link-item {
  margin-bottom: 20px;
}
</style>
